package com.ding.custommanagersys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ding.custommanagersys.entity.Custom;
import com.ding.custommanagersys.entity.User;
import com.ding.custommanagersys.mapper.UserMapper;
import com.ding.custommanagersys.service.ICustomService;
import com.ding.custommanagersys.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ding.custommanagersys.vo.CustomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ding
 * @since 2023-04-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    @Lazy
    private ICustomService customService;

    @Override
    public User userExist(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("t_username",username);
        return getOne(wrapper);
    }

    @Override
    public int userRegister(String username, String password) {
        User user = new User();
        user.settUsername(username);
        user.settPassword(password);
        String maxUserId = userMapper.getMaxUserId();
        if(maxUserId == null){
            maxUserId = "360000";
        }
        int id = Integer.parseInt(maxUserId) + 1;
        user.settUserId(String.valueOf(id));

        CustomVo custom = new CustomVo();
        custom.setNumber(String.valueOf(id));
        custom.setMemberId(1000);
        custom.setProvince("");
        custom.setCity("");
        custom.setCounty("");
        customService.addCustomByRegister(custom);

        return userMapper.insert(user);
    }

    @Override
    public User userLogin(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("t_username",username);
        wrapper.eq("t_password",password);
        return getOne(wrapper);
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int deleteByIds(List<String> ids) {
        return userMapper.deleteBatchIds(ids);
    }
}
